package com.njupt.webapp.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author PengJu Ma
 *
 * 2007-1-29
 */
public class ShortItTag extends TagSupport {

	private String str;
	private String len;
	private String fix;
	private String scope;
	
	public String getScope() {
		return scope;
	}
	public void setScope(String scope) {
		this.scope = scope;
	}
	public void setStr(String strEl){
		this.str = strEl;
	}
	public void setLen(String lenEl){
		this.len = lenEl;
	}
	public void setFix(String fix){
		this.fix = fix;
	}
	public int doStartTag() throws JspException {
		String s = (String)TagUtils.evaluate("str",str,String.class, this, pageContext); 
		Integer lenObject = (Integer) TagUtils.evaluate("len", len, Integer.class, this, pageContext);
		int lenInt = lenObject.intValue();
        String ss=s;
		if (s != null && s.length() > lenInt) {
			if(fix == null)fix = ("...");
			ss = substring(s,  lenInt,
					fix);
		}
		TagUtils.saveToContext(this,pageContext,scope,ss);
		return super.doStartTag();
	}
	/**
	 * 
	 * 其实这个标签是有问题的，对于小的长度的截取是不行的
	 * @param str
	 * @param from
	 * @param to
	 * @return
	 */
	public static void main(String[] args){
		String ss = substring("中国人我们计算错误来的哈哈",4,"");
		System.out.println(ss);
	}
	public static String substring(String str, int length,String postFix){
		if (str == null) {
			return null;
		}
		length = length * 2;
		int len = strLength2(str);
		if(len <= length)return str;
		char[] chars = str.toCharArray();
		len = length - strLength2(postFix);
		int x = 0;
		int j = 0;
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			if((int)c > 255)x+=2;
			else x++;
			j++;
			if(x >= len)break;
		}
		String newStr = new String(chars,0,j);
		newStr += postFix;
		return newStr;
	}
		//newStr.substring(from, to);
//		try {
//			if (chartsetNow == null) {
//				newStr = new String(str.getBytes(), charsetDisplay);
//			} else {
//				newStr = new String(str.getBytes(chartsetNow), charsetDisplay);
//			}
//
//		} catch (UnsupportedEncodingException e) {
//		}
//		try {
//			if (newStr.length() > to) {
//				newStr = newStr.substring(from, to);
//			}
//			if (chartsetNow == null) {
//				return new String(newStr.getBytes(charsetDisplay));
//			} else {
//				return new String(newStr.getBytes(charsetDisplay), chartsetNow);
//			}
//
//		} catch (UnsupportedEncodingException e1) {
//		}
//	}
	/*
	 * 汉字Unicode编码的区间为：Ox4E00->Ox9FA5
	 * 截取str前n个字符+postFix作为返回值
	 */
	public static String substring2(String str,int n,String postFix)
    {
       
      String temp= "";
      if(strLength2(str)<n){
       return str;
      }else{
                    int t=0;
                    char[] tempChar=str.toCharArray();
                    for(int i=0;i<tempChar.length&&t<n;i++)
                    {
                            if((int)tempChar[i]>=0x4E00 && (int)tempChar[i]<=0x9FA5)//
                            {
                                    temp+=tempChar[i];
                                    t+=2;
                            }
                            else
                            {
                                    temp+=tempChar[i];
                                    t++;
                            }
                    }
                    return (temp+=postFix);
            }
    } 
	// 统计字符str的长度
	public static int strLength2(String str){
		if(str == null)return -1;
		char[] chars = str.toCharArray();
		int len = 0;
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			if(c > 255){
				len+=2;
			}else{
				len++;
			}
		}
		return len;
	}
}
